package com.tedu.ds.customer.controller;

import com.tedu.ds.base.response.JsonResult;
import com.tedu.ds.customer.pojo.dto.CustomerLoginParam;
import com.tedu.ds.customer.pojo.dto.CustomerParam;
import com.tedu.ds.customer.pojo.dto.CustomerQuery;
import com.tedu.ds.customer.pojo.vo.CustomerVO;
import com.tedu.ds.customer.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/v1/customer")
@Api(tags = "客户模块")
public class CustomerController {
    @Autowired
    CustomerService customerService;

    @PostMapping("/save")
    @ApiOperation("新增和更新功能")
    public JsonResult save(@RequestBody CustomerParam customerParam){
        customerService.save(customerParam);
        return JsonResult.ok();
    }
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除功能")
    @ApiImplicitParam(name = "id",value = "客户id",required = true)
    public JsonResult deleteById(@PathVariable Long id){
        customerService.deleteById(id);
        return JsonResult.ok();
    }
    @PostMapping("/select")
    @ApiOperation("查询功能")
     public JsonResult selectById(@RequestBody CustomerQuery customerQuery){
        List<CustomerVO> list=customerService.select(customerQuery);
        return JsonResult.ok(list);
    }

    @PostMapping("/login")
    @ApiOperation("登录功能")
    public JsonResult login(@RequestBody CustomerLoginParam loginParam, @ApiIgnore HttpSession session){
        CustomerVO customerVO=customerService.login(loginParam);
        session.setAttribute("customerVO",customerVO);
        return JsonResult.ok();
    }

    @GetMapping("/logout")
    @ApiOperation("登出功能")
    public JsonResult logout(@ApiIgnore HttpSession session){
        session.removeAttribute("customerVO");
        session.invalidate();
        return JsonResult.ok();
    }

    @GetMapping("/currentCustomer")
    @ApiOperation("查询登录信息")
    public JsonResult currentCustomer(@ApiIgnore HttpSession session){
        CustomerVO customerVO=(CustomerVO) session.getAttribute("customerVO");
        return JsonResult.ok(customerVO);
    }

    @GetMapping("/select/{username}")
    @ApiOperation("根据用户名获取用户信息")
    public JsonResult selectByUsername(@PathVariable String username){
        return JsonResult.ok(customerService.selectByUsername(username));
    }




}
